//
//  BZVisitorView.swift
//  weibo
//
//  Created by zzx on 2017/7/23.
//  Copyright © 2017年 zzx. All rights reserved.
//

import UIKit
import SnapKit

//创建访客视图代理
protocol BZVisitorViewDelegate: NSObjectProtocol {

    func loginOperation()

}


class BZVisitorView: UIView {
    
    //点击登录注册按钮
    var loginCallBack: (()->())?
    //接收代理对象
    weak var delegate: BZVisitorViewDelegate?

    //懒加载
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var maskIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var homeImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var titleLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "关注一些人,回这里看看什么惊喜"
        
        label.textColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
        
        label.textAlignment = .center
        
        label.font = UIFont.systemFont(ofSize: 14)
        
        label.numberOfLines = 0
        
        return label
    
    }()
    
    ///注册按钮
    private lazy var registerButton: UIButton = {
    
        let button = UIButton()
        
        button.setTitle("注册", for: .normal)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        
        button.setTitleColor(#colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1), for: .normal)
        
        button.setTitleColor(#colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1), for: .highlighted)
        
        
        let edgeInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        
        let image = UIImage(named:"common_button_white_disable")?.resizableImage(withCapInsets:  edgeInset)
        
        button.setBackgroundImage(image, for: .normal)
        
        button.adjustsImageWhenHighlighted = false
        
        //按钮的点击事件
        button.addTarget(self, action:#selector(registionAction) , for: .touchUpInside)
    
        return button
    
    }()
    
    ///登陆按钮
    private lazy var logInButton: UIButton = {
        
        let button = UIButton()
        
        button.setTitle("登陆", for: .normal)
        
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        
        button.setTitleColor(#colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1), for: .normal)
        
        button.setTitleColor(#colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1), for: .highlighted)
        
        
        let edgeInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
        
        let image = UIImage(named:"common_button_white_disable")?.resizableImage(withCapInsets:  edgeInset)
        
        button.setBackgroundImage(image, for: .normal)
        
        button.adjustsImageWhenHighlighted = false
        
        //按钮的点击事件
        button.addTarget(self, action:#selector(loginAction) , for: .touchUpInside)
        
        
        return button
        
    }()
    
    @objc private func registionAction() {
        
        //执行闭包回调
        loginCallBack?()
        
        //使用代理对象调用方法完成回调
        delegate?.loginOperation()
        
    }
    
    @objc private func loginAction() {
        
        //执行闭包回调
        loginCallBack?()
        
        //使用代理对象调用方法完成回调
        delegate?.loginOperation()
        
        
        
    }
    
    
    //MARK:- 修改访问视图信息
    func updateVisitorInfo(message: String?, imageName: String?){
        
        if let msg = message, let imageName = imageName {
            //这不是首页
            //隐藏旋转视图,并且修改其他信息
            cycleImageView.isHidden = true
            
            titleLabel.text = msg
            
            homeImageView.image = UIImage(named: imageName)
            
            
            
        }else{
            
            //开启动画
            rotationAnimation()
            
        }
        
        
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        fatalError("init(coder:) has not been implemented")
        
    }
    
    //MARK:- 设置UI界面
    func setupUI() {
        
        backgroundColor = UIColor(white: 237.0 / 255, alpha: 1.0)
        
        addSubview(cycleImageView)
        addSubview(maskIconImageView)
        addSubview(homeImageView)
        addSubview(titleLabel)
        addSubview(registerButton)
        addSubview(logInButton)
        
        cycleImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(self)
            
        }
        
        maskIconImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(cycleImageView)
            
        }
        
        homeImageView.snp.makeConstraints { (make) in
            
            make.center.equalTo(maskIconImageView)
            
        }
        
        titleLabel.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(homeImageView)
            
            make.top.equalTo(homeImageView.snp.bottom).offset(10)
            
            make.width.equalTo(224)
            
        }
        
        registerButton.snp.makeConstraints { (make) in
            
            make.leading.equalTo(titleLabel)
            
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            
            make.size.equalTo(CGSize(width: 80, height: 35))
            
        }
        
        
        logInButton.snp.makeConstraints { (make) in
            
            make.trailing.equalTo(titleLabel)
            
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            
            make.size.equalTo(CGSize(width: 80, height: 35))
            
        }
        rotationAnimation()
        
    }
    
    //MARK:- 设置旋转动画
    private func rotationAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        //动画目的
        animation.toValue = 2 * Double.pi
        
        //设置时长
        animation.duration = 20
        
        //设置重复次数
        animation.repeatCount = MAXFLOAT
        //在执行次数内失去焦点
        animation.isRemovedOnCompletion = false
        
        //执行动画
        cycleImageView.layer.add(animation, forKey: nil)
        
    }
    
    
    
}










